package com.fengyunhe.manager.web;


import com.fengyunhe.manager.service.AdminRoleService;
import com.fengyunhe.manager.vo.CheckableRoleVo;
import com.fengyunhe.common.bean.Page.Offset;
import com.fengyunhe.common.bean.PageList;
import com.fengyunhe.manager.AdminLog;
import com.fengyunhe.manager.CurrentUser;
import com.fengyunhe.manager.service.AdminUserService;
import com.fengyunhe.manager.shiro.UserRole;
import com.fengyunhe.manager.vo.AdminUserVo;
import com.fengyunhe.manager.vo.RoleVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户管理
 */
@Controller
@RequestMapping("/admin/adminUser")
public class AdminUserController extends AdminBaseController {

    private static final String MODULE_ID = "manager.user";
    private static final String MODULE_NAME = "后台用户管理";
    @Resource
    private AdminRoleService adminRoleService;

    @Autowired
    private AdminUserService adminUserService;


    /**
     * 后台用户帐号分页列表
     *
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping("/list.do")
    @RequiresPermissions(MODULE_ID + ":view")
    public Map<String, Object> list(Offset page, String keywords, String usertype,Integer roleId) {
        Map<String, Object> map = new HashMap<String, Object>();

        boolean success = false;
        String msg = null;

        try {
            PageList<AdminUserVo> pageList = null;
            //pageList = this.adminUserService.findByPage(page, keywords, usertype);
            pageList = this.adminUserService.getUserPageListByRoleId(roleId,usertype,keywords,page);
            map.put("pageList", pageList);
            success = true;
        } catch (Exception e) {
            success = false;
            log.error("查询用户列表出错", e);
            msg = e.getMessage();
        }
        map.put("success", success);
        map.put("msg", msg);

        return map;
    }

    /**
     * 创建后台用户帐号
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/add.do")
    @RequiresPermissions(MODULE_ID + ":add")
    @AdminLog(module = MODULE_NAME, action = "添加用户")
    public Map<String, Object> add(AdminUserVo user) {
        Map<String, Object> map = new HashMap<String, Object>();

        boolean success = false;
        String msg = null;
        try {
            this.adminUserService.addAdminUser(user);
            success = true;
        } catch (Exception e) {
            log.error("添加用户出错", e);
            success = false;
            msg = e.getMessage();
        }
        map.put("success", success);
        map.put("msg", msg);
        return map;
    }

    /**
     * 修改后台用户帐号
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/update.do")
    @RequiresPermissions(MODULE_ID + ":update")
    @AdminLog(module = MODULE_NAME, action = "修改用户")
    public Map<String, Object> update(
            AdminUserVo user) {
        Map<String, Object> map = new HashMap<String, Object>();

        boolean success = false;
        String msg = null;
        try {
            this.adminUserService.updateAdminUser(user);
            success = true;
        } catch (Exception e) {
            log.error("修改用户出错", e);
            success = false;
            msg = e.getMessage();
        }
        map.put("success", success);
        map.put("msg", msg);
        return map;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/remove.do")
    @RequiresPermissions(MODULE_ID + ":remove")
    @RequiresRoles(UserRole.SUPERMAN)   //超级用户身份验证
    @AdminLog(module = MODULE_NAME, action = "删除用户")
    public Map<String, Object> remove(Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        String msg = null;
        Integer curr = CurrentUser.get().getUserId();
        if (curr.intValue() == userId) {
            map.put("success", false);
            map.put("msg", "不能删除您登录的账号");
            return map;
        }
        boolean success = false;
        try {
            this.adminUserService.deleteAdminUser(userId);
            success = true;
        } catch (Exception e) {
            log.error("删除用户出错", e);
            success = false;
            msg = e.getMessage();
        }
        map.put("success", success);
        map.put("msg", msg);
        return map;
    }

    /**
     * 停用用户
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/disable.do")
    @RequiresPermissions(MODULE_ID + ":disable")
    @AdminLog(module = MODULE_NAME, action = "停用用户")
    public Map<String, Object> disable(Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        String msg = null;
        Integer curr = CurrentUser.get().getUserId();
        if (curr.intValue() == userId) {
            map.put("success", false);
            map.put("msg", "不能操作您自己的帐号");
            return map;
        }
        boolean success = false;
        try {
            this.adminUserService.updateUserToDisabled(userId);
            success = true;
        } catch (Exception e) {
            log.error("停用用户出错", e);
            success = false;
            msg = e.getMessage();
        }
        map.put("success", success);
        map.put("msg", msg);
        return map;
    }

    /**
     * 启用用户
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/enable.do")
    @RequiresPermissions(MODULE_ID + ":enable")
    @AdminLog(module = MODULE_NAME, action = "启用用户")
    public Map<String, Object> enable(Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        String msg = null;
        Integer curr = CurrentUser.get().getUserId();
        if (curr.intValue() == userId) {
            map.put("success", false);
            map.put("msg", "不能操作您自己的帐号");
            return map;
        }
        boolean success = false;
        try {
            this.adminUserService.updateUserToEnabled(userId);
            success = true;
        } catch (Exception e) {
            log.error("停用用户出错", e);
            success = false;
            msg = e.getMessage();
        }
        map.put("success", success);
        map.put("msg", msg);
        return map;
    }

    /**
     * 授权操作界面，查询用户的角色列表。如果用户已经具有此角色，则为选中状态
     *
     * @param userId
     * @return
     */
    @RequestMapping("/roleList.do")
    @ResponseBody
    @RequiresPermissions(MODULE_ID + ":grant")
    public Map<String, Object> roleList(Integer userId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<RoleVo> roleList = adminRoleService.getAllRoleList();
        List<Integer> userRoleIdList = this.adminRoleService.getRoleIdListByAdminUserId(userId);

        if (roleList != null) {
            List<CheckableRoleVo> checkableRoleVoList = new ArrayList<CheckableRoleVo>();
            for (RoleVo role : roleList) {
                CheckableRoleVo checkableRoleVo = new CheckableRoleVo(role);
                checkableRoleVoList.add(checkableRoleVo);
                //判断用户是否具有此角色
                if (userRoleIdList != null && userRoleIdList.contains(checkableRoleVo.getRoleId())) {
                    checkableRoleVo.setChecked(true);
                }
            }
            map.put("list", checkableRoleVoList);
            map.put("success", true);
        } else {
            map.put("list", null);
            map.put("success", true);
        }
        return map;
    }


    /**
     * 授权用户角色
     *
     * @param userId
     * @param roleId
     * @return
     */
    @RequestMapping("/grant.do")
    @ResponseBody
    @RequiresPermissions(MODULE_ID + ":grant")
    @AdminLog(module = MODULE_NAME, action = "授权角色")
    public Map<String, Object> grant(Integer userId, Integer[] roleId) {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean updateAccountRoles = this.adminUserService.updateAdminUserRoles(userId, roleId);
        map.put("success", updateAccountRoles);
        return map;
    }


}
